package org.aeon.client.view.components.button;

import lombok.Getter;
import org.aeon.client.view.components.sessionPanel.GroupChatPanel;
import org.aeon.client.view.components.sessionPanel.SessionPanel;
import org.aeon.client.view.components.sessionPanel.UserChatPanel;
import org.aeon.entity.dto.GroupInfoDto;
import org.aeon.entity.dto.UserInfoDto;

import javax.swing.*;
import java.awt.*;

/**
 * @author: MissingAeon
 * @date: 2024/2/20 16:17
 * @description:
 */
public class ActiveContactButton extends JButton {
    private final JLabel nameLabel;
    private final JLabel timeLabel;
    @Getter
    private Object content;

    public ActiveContactButton(GroupInfoDto group, String time, SessionPanel sessionPanel) {
        this(group.getGroupName(), time);
        this.content = group;
        this.addActionListener(e -> {
            final GroupChatPanel panel = new GroupChatPanel();
            panel.fillInData(group);
            sessionPanel.add(panel);
        });
    }

    public ActiveContactButton(UserInfoDto user, String time, SessionPanel sessionPanel) {
        this(user.getNickname(), time);
        this.content = user;
        this.addActionListener(e -> {
            final UserChatPanel panel = new UserChatPanel();
            panel.fillInData(user);
            sessionPanel.add(panel);
        });
    }


    private ActiveContactButton(String name, String time) {
        this.setPreferredSize(new Dimension(275, 75));
        this.setContentAreaFilled(false);

        JPanel butContent = new JPanel(null); // 使用绝对布局
        // butContent.setBackground(Color.RED);

        // 创建姓名标签
        nameLabel = new JLabel(name);
        nameLabel.setBounds(5, 15, 180, 30); // 设置姓名标签的位置和大小
        nameLabel.setFont(new Font("黑体", Font.BOLD, 22));
        nameLabel.setHorizontalAlignment(SwingConstants.LEFT); // 文字左对齐

        // 创建时间标签
        timeLabel = new JLabel(time);
        Font font = timeLabel.getFont();
        timeLabel.setFont(new Font("黑体", Font.PLAIN, 12));
        timeLabel.setBounds(280 - 180, 50, 125, 14); // 设置时间标签的位置和大小
        timeLabel.setHorizontalAlignment(SwingConstants.RIGHT); // 文字右对齐

        // 向面板中添加组件
        butContent.add(nameLabel);
        butContent.add(timeLabel);

        this.setLayout(new BorderLayout()); // 设置按钮的布局管理器为边界布局
        this.add(butContent, BorderLayout.CENTER); // 将面板添加到按钮的中心位置
    }

    /**
     * 修改按钮显示名称
     *
     * @param text
     */
    public void setNameText(String text) {
        nameLabel.setText(text);
    }

    /**
     * 修改按钮显示时间
     *
     * @param text
     */
    public void setTimeText(String text) {
        timeLabel.setText(text);
    }
}
